import org.junit.Test;

/**
 * @author wly
 * @version V1.0.0
 * @date 2023/3/17 21:24
 *
 * 递归实现二分查找发
 */
public class BinarySearch {
    public BinarySearch(){}

    public static <E extends Comparable<E>> int search(E[] data, E target){
        return search(data, 0, data.length - 1, target);
    }

    private static <E extends Comparable<E>> int search(E[] data, int l, int r, E target){
        if (l > r){
            return -1;
        }
        int mid = l + (r - l) / 2;
        if (data[mid].compareTo(target) == 0){
            return mid;
        }
        if (data[mid].compareTo(target) < 0){
            return search(data, mid + 1, r, target);
        }
        return search(data, l, mid - 1, target);
    }

    @Test
    public void test01(){
        Integer[] nums = {1, 3, 4, 7, 9, 12};
        int target = 10;
        int index = search(nums, target);
        System.out.println(index);
    }
}
